package com.springboot.blog.controller;

import com.alibaba.fastjson.JSONObject;
import com.springboot.blog.beans.BlogShop;
import com.springboot.blog.beans.YouQing;
import com.springboot.blog.config.utils.R;
import com.springboot.blog.dao.ShopVipDao;
import com.springboot.blog.service.VipShopServiceImpl;
import net.sf.json.JSONArray;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class ShopVipController {

    @Resource
    private ShopVipDao shopVipDao;
    @Resource
    private VipShopServiceImpl vipShopService;


    @RequestMapping("addVip")
    public R addVip(String vipname,String vipcontent,String vipprice,String vipimage){
        System.out.println("2222222222"+vipprice);
        BlogShop blogShop = new BlogShop();
        blogShop.setShopName(vipname);
        blogShop.setShopContent(vipcontent);
        blogShop.setShopPrice(Double.valueOf(vipprice));
        blogShop.setShopImage(vipimage);
        int insert = shopVipDao.insert(blogShop);
        if (insert>0){
            return R.ok();
        }
        return R.error();
    }

    @RequestMapping("buyVip")
    public R buyVip(HttpServletRequest request, String shopId){
        String  userName = (String) request.getSession().getAttribute("userName");
        return vipShopService.buyVipshop(userName, Integer.parseInt(shopId));
    }

    @RequestMapping("allVip")
    public String allVip(){
        List<BlogShop> blogShops = shopVipDao.selectList(null);
        Integer count = shopVipDao.selectCount(null);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("count",count);
        jsonObject.put("data",blogShops);
        return jsonObject.toString();
    }

    @RequestMapping("deletevip")
    public R deletevip(String delete){
        List<BlogShop> list2=(List<BlogShop>) JSONArray.toList(JSONArray.fromObject(delete), BlogShop.class);
        List<Integer> userList = new ArrayList<>();
        for (BlogShop blogShop : list2){
            userList.add(blogShop.getId());
        }
        int deleteUser = shopVipDao.deleteBatchIds(userList);
        if (deleteUser>0) {
            return R.ok();
        }
        return R.error();
    }

    @RequestMapping("deleteByIdvip")
    public R deleteByIdvip(String id){
        int delete = shopVipDao.deleteById(Integer.parseInt(id));
        if (delete>0){
            return R.ok();
        }
        return R.error();
    }

    @RequestMapping("updateByIdvip")
    public R updateByIdvip(String id,String vipname,String vipcontent,String vipprice,String vipimage){
        BlogShop blogShop = new BlogShop();
        blogShop.setId(Integer.parseInt(id));
        blogShop.setShopName(vipname);
        blogShop.setShopContent(vipcontent);
        blogShop.setShopPrice(Double.valueOf(vipprice));
        blogShop.setShopImage(vipimage);
        int update = shopVipDao.updateById(blogShop);
        if (update>0){
            return R.ok();
        }
        return R.error();
    }

}
